package top.zhmq.userconsumer.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * 发送验证码邮件
 *
 * @author Glory
 * @classname MailUtil
 * @create 2021-06-02 10:37
 */

@Component
public class MailUtil {
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private String MAIL_FROM;
    @Value("${spring.mail.password}")
    private String MAIL_FROM_PASSWORD;

    private static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";//邮件的传输协议
    private static final String MAIL_TRANSPORT_PROTOCOL_VALUE = "smtp";//使用smtp协议
    private static final String MAIL_HOST = "mail.host";//发送邮件的主机
    private static final String MAIL_HOST_VALUE = "smtp.qq.com"; //发送邮件的服务器地址
    private static final String MAIL_DEBUG = "mail.debug"; //调试模式
    private static final String MAIL_SMTP_AUTH = "mail.smtp.auth";//邮件smtp作者确认
    private static final String CONFIRM = "true";//确认
    private static final String SEND_ENCODING_LAYOUT = "text/html;charset=utf-8";//发送邮件的编码格式

    private static final String MAIL_SUBJECT_REGISTER = "个人博客-游客【注册链接】";//邮件主题(注册链接)
    private static final String MAIL_SUBJECT_VERIFY_CODE = "模拟商城-顾客【邮箱验证码】";//邮件主题(邮箱验证码)
    private static final String MAIL_ORGANIZATION = "【模拟商城】";//邮件组织
    private static final String MAIL_WEBSITE = "https://zhmq.top";//组织主页
    private static final String MAIL_ORGANIZATION_LOGO = "https://blog.zhmq.top/wp-content/uploads/2021/03/2021030714332313.png";//网站logo
    private static final String MAIL_ORGANIZATION_QRCODE = "https://blog.zhmq.top/wp-content/uploads/2021/06/2021060203065318.png";//网站二维码图片

    // 发送简单邮件
    public void sendSimpleEmail(String mailto, String title, String content) {
        //  定制邮件发送内容
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(MAIL_FROM);
        message.setTo(mailto);
        message.setSubject(title);
        message.setText(content);
        // 发送邮件
        mailSender.send(message);
    }

    public String sendAEmail(String mailto, String code) throws Exception {
        String[] strings = new String[]{
                "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m",
                "A", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M",
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        StringBuilder verifyCode = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            verifyCode.append(strings[(int) Math.floor(Math.random() * strings.length)]);
        }
        sendEmail(MAIL_FROM,
                mailto,
                MAIL_SUBJECT_VERIFY_CODE,
                prettyLayout(MAIL_ORGANIZATION, MAIL_WEBSITE,
                        MAIL_ORGANIZATION_LOGO,
                        prettyQrCodeLayout("15分钟", String.valueOf(code)),
                        MAIL_ORGANIZATION_QRCODE),
                MAIL_FROM_PASSWORD);
        return String.valueOf(verifyCode);
    }

    /**
     * 发送邮件
     *
     * @param from     发送人(邮箱地址)
     * @param to       接收人  (邮箱地址)
     * @param subject  主题
     * @param content  内容
     * @param password 密码(密码为授权码不是邮箱的登录密码)
     */
    public static void sendEmail(String from, String to, String subject, String content, String password) throws Exception {
        Properties props = new Properties();//key value:配置参数。真正发送邮件时再配置
        props.setProperty(MAIL_TRANSPORT_PROTOCOL, MAIL_TRANSPORT_PROTOCOL_VALUE);//指定邮件发送的协议，参数是规范规定的
        props.setProperty(MAIL_HOST, MAIL_HOST_VALUE);//指定发件服务器的地址，参数是规范规定的
//        props.setProperty(MAIL_DEBUG, "true");//邮件发送的调试模式，参数是规范规定的
        props.setProperty(MAIL_SMTP_AUTH, CONFIRM);//请求服务器进行身份认证。参数与具体的JavaMail实现有关
        Session session = Session.getInstance(props);//发送邮件时使用的环境配置
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        //设置邮件的头
        message.setFrom(new InternetAddress(from)); //谁发送的
        message.setRecipients(Message.RecipientType.TO, to);//发送给谁
        message.setSubject(subject);
        //设置正文
        message.setContent(content, SEND_ENCODING_LAYOUT);
//        message.setText("<h1>hello</h1>");//纯文本
        message.saveChanges();

        //发送邮件
        Transport ts = session.getTransport();
        ts.connect(from, password);       // 密码为授权码不是邮箱的登录密码
        ts.sendMessage(message, message.getAllRecipients());//对象，用实例方法}
    }

    /**
     * 邮件内容头部
     *
     * @param websiteName    网站名称
     * @param principal      网站主页F
     * @param principalImage 网站logo图片超链接
     * @param logoWidth      logo图片宽度
     * @param logoHeight     logo图片高度
     * @return
     */
    private static String mailHead(String websiteName, String principal, String principalImage, String logoWidth, String logoHeight) {
        return "<div style=\"margin: 0 auto; text-align: left;\n" +
                "padding: 38px 50px; width: 560px; font-size: 14px;\n" +
                "color: #606060; background: #fff; border-radius:\n" +
                "2px; font-family:Tahoma,Helvetica,'microsoft\n" +
                "yahei','Hiragino Sans GB',Simsun,sans-serif;\n" +
                "box-shadow: 0 0 4px rgba(0,0,0,0.2);\">\n" +
                "        <table style=\"margin: 0 auto;\n" +
                "text-align: left; font-size: 14px; color: #606060;\n" +
                "background: #fff; font-family: inherit;\n" +
                "font-family:Tahoma,Helvetica,'microsoft\n" +
                "yahei','Hiragino Sans GB',Simsun,sans-serif;\" width=\"560\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n" +
                "            <tbody><tr>\n" +
                "                <th colspan=\"2\"><a href=\"" + principal + "\" rel=\"noopener\" target=\"_blank\"><img src=\"" + principalImage + "\" width=\"" + logoWidth + "\" height=\"" + logoHeight + "\"></a></th>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <td colspan=\"2\" style=\"font-size:\n" +
                "18px; padding: 30px 0\n" +
                "18px;\">尊敬的" + websiteName + "用户：</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <td colspan=\"2\" style=\"line-height: 1.8;\">\n" +
                "                   \n" +
                "<div>欢迎使用" + websiteName + "！</div>\n";
    }

    /**
     * 邮件内容脚部
     *
     * @param websiteName 网站名称
     * @param principal   网站主页
     * @param qrCode      二维码图片链接(可不填写/仅支持第一张)
     * @return
     */
    private static String mailFoot(String websiteName, String principal, String... qrCode) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String nowDate = sdf.format(new Date());
        return "            <tr>\n" +
                "                <td colspan=\"2\" style=\"text-align:\n" +
                "right; line-height: 1.8; padding-bottom: 18px;\">\n" +
                "                    <div>" + websiteName + "团队</div>\n" +
                "                    <div style=\"color:\n" +
                "#909090;\">" + nowDate + "</div>\n" +
                "                </td>\n" +
                "            </tr>\n" +
                (qrCode != null && qrCode.length != 0 ? setQrCode(websiteName, qrCode[0]) : "") +
                "            <tr>\n" +
                "                <td colspan=\"2\" style=\"padding-top: 20px; border-top: 1px solid\n" +
                "#e7e7e7; line-height: 1.8; font-size: 12px; color:\n" +
                "#909090;\">\n" +
                "                    <div>温馨提示：</div>\n" +
                "                    <div>1.\n" + websiteName +
                "网址为：<a href=\"" + principal + "\" rel=\"noopener\" target=\"_blank\">" + principal + "</a>，请注意网址，防止钓鱼。</div>\n" +
                "                    <div>2.\n" +
                "本邮件为系统自动发出，请勿回复。</div>\n" + " <div>3.\n该系统仅为学习使用。" +
                "                </td>\n" +
                "            </tr>\n" +
                "        </tbody></table>\n" +
                "    </div>";
    }

    /**
     * 输出漂亮格式(带logo,可带二维码)
     *
     * @param websiteName    网站名称
     * @param principal      主页地址
     * @param principalImage 主页logo 323 x 48 拉伸宽度为 (超链接形式)
     * @param business       业务
     * @param qrCode         二维码 (超链接形式)
     */
    private static String prettyLayout(String websiteName, String principal, String principalImage, String business, String... qrCode) {
        return mailHead(websiteName, principal, principalImage, "", "") +
                business +
                mailFoot(websiteName, principal, qrCode);
    }

    /**
     * 二维码 样式A
     *
     * @param websiteName //网站名称
     * @param qrCode      //二维码图片链接
     * @return
     */
    private static String setQrCode(String websiteName, String qrCode) {
        return "            <tr>\n" +
                "                <td style=\"padding-bottom: 20px;\" width=\"145\">\n" +
                "                    <img src=\"" + qrCode + "\" width=\"110\" height=\"112\">\n" +
                "                </td>\n" +
                "                <td style=\"line-height: 2;\n" +
                "padding-bottom: 20px;\">\n" +
                "                    <div style=\"font-size: 18px;\n" +
                "color:\n" +
                "#03c5ff;\">扫描关注" + websiteName + "公众号</div>\n" +
                "                </td>\n" +
                "            </tr>\n";
    }

    /**
     * 发送邮箱验证码
     *
     * @param validity   有效期/单位自行填写
     * @param verifyCode 验证码
     * @return
     */
    private static String prettyQrCodeLayout(String validity, String verifyCode) {
        return "<div>邮箱验证码的有效期为:" + validity + "\n邮箱验证码如下:</div>\n" +
                "                </td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <td colspan=\"2\" style=\"font-size:\n" +
                "12px; line-height: 20px; padding-top: 14px;\n" +
                "padding-bottom: 25px; color: #909090;\">\n" +
                "                    <div>" + verifyCode + "</div>\n" +
                "                </td>\n" +
                "            </tr>\n";
    }

    /**
     * 发送邮箱注册激活链接
     *
     * @param validity    有效期/单位自行填写
     * @param websiteName 网站名
     * @param verifyLink  注册验证链接
     * @return
     */
    private static String prettyRegisterLayout(String validity, String websiteName, String verifyLink) {
        return "<div>请点击以下的链接验证您的邮箱，验证成功后就可以使用" + websiteName + "提供的服务了。</div>\n" +
                "                </td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <td colspan=\"2\" style=\"font-size:\n" +
                "12px; line-height: 20px; padding-top: 14px;\n" +
                "padding-bottom: 25px; color: #909090;\">\n" +
                "                    <div>该链接的有效期为" + validity + "，如链接超过有效期请重新发送邮件<a href=\"" + verifyLink + "\" style=\"color: #03c5ff; text-decoration:\n" +
                "underline;\" rel=\"noopener\" target=\"_blank\">" + verifyLink + "</a></div>\n" +
                "                    <div style=\"padding-top:\n" +
                "4px;\">(如果不能打开页面，请复制该地址到浏览器打开)</div>\n" +
                "                </td>\n" +
                "            </tr>\n";
    }
}

